﻿Imports DTO
Imports BUS
Public Class frmCHITIETPHIEUNHAP

    Private IsInitializing As Boolean

    Private Sub btt_thoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub
    Dim drw As DataRow
    Dim dt As New PhieuNhapBUS
    Private Sub frmCHITIETPHIEUNHAP_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDsPN()
        loadcbbkho()
        loadcbbsp()
    End Sub
    Private Sub loadcbbsp()
        Dim spbus As New MatHangBUS()
        cbo_SP.DataSource = spbus.LoadMH()
        cbo_SP.DisplayMember = "TenSP"
        cbo_SP.ValueMember = "MaSP"
    End Sub
    Private Sub loadcbbkho()
        Dim khobus As New KhoBUS()
        cbo_TenKho.DataSource = khobus.LAYTATCA()
        cbo_TenKho.DisplayMember = "TenKho"
        cbo_TenKho.ValueMember = "MaKho"
    End Sub
    Dim pnbus As New PhieuNhapBUS
    Private Sub LoadDsPN()
        Dim dt As DataTable = New DataTable
        dt = pnbus.LayTTPN()
        dgv_chitiet.DataSource = dt
    End Sub


    Private Sub btn_thoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub btn_them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_them.Click
        If txt_SoLuong.Text <> "" And cbo_SP.Text <> "" Then
            themsp()
            LoadDsPN()
            MessageBox.Show("Đã Thêm Thành Công!")
        Else
            MessageBox.Show("Xin Nhap Day Du Thong Tin!", "Thông Báo")
        End If
    End Sub
    Private Sub themsp()

        Dim pndto As New PhieuNhapDTO()
        pndto.MaSP = Integer.Parse(cbo_SP.SelectedValue)
        pndto.NgayNhap = DateTime.Parse(dtp_ngaynhap.Value)
        pndto.SoLuong = Integer.Parse(txt_SoLuong.Text)
        pndto.MaKho = Integer.Parse(cbo_TenKho.SelectedValue)
        PhieuNhapBUS.THEM(pndto)
    End Sub

    Private Sub btn_xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_xoa.Click
        Dim dial = MessageBox.Show("Bạn Có Thực Sự Muốn Xóa Phiếu Nhập Kho Này Không", "Hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dial = DialogResult.Yes Then
            Dim nv As New PhieuNhapDTO()
            nv.MaPN = dgv_chitiet.Rows(temp).Cells(0).Value
            PhieuNhapBUS.XOA(nv.MaPN)
            LoadDsPN()
            MessageBox.Show("Da Xoa Thanh Cong!")
        End If
    End Sub

    Private Sub btn_thoat_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_thoat.Click
        Me.Close()
    End Sub

    Private Sub btn_sua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_sua.Click
        Dim dial = MessageBox.Show("Bạn Có Thực Sự Muốn Cập Nhập Phiếu Nhập Này không",
                                  "Hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dial = DialogResult.Yes Then
            Dim nv As New PhieuNhapDTO
            nv.MaKho = Integer.Parse(cbo_TenKho.SelectedValue)
            nv.NgayNhap = DateTime.Parse(dtp_ngaynhap.Value)
            nv.SoLuong = Integer.Parse(txt_SoLuong.Text)
            nv.MaSP = Integer.Parse(cbo_SP.SelectedValue)
            nv.MaPN = dgv_chitiet.Rows(temp).Cells(0).Value
            PhieuNhapBUS.CAPNHAP(nv)
            LoadDsPN()
            MessageBox.Show("Da Cap Nhap Thanh Cong!")
        End If
    End Sub

    Private Sub btn_Nhaplai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Nhaplai.Click
        txt_SoLuong.Text = ""
        cbo_SP.Text = ""
        cbo_TenKho.Text = ""
        dtp_ngaynhap.Text = ""
    End Sub
    Dim temp As Integer
    Private Sub dgv_chitiet_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv_chitiet.RowEnter
        temp = e.RowIndex
    End Sub
End Class